/*
* 添加处罚人员
* */

import 'package:center_beijing/api/task_api.dart';
import 'package:center_beijing/common/logutil.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:center_beijing/models/selectItem.dart';
import 'package:center_beijing/widgets/custom_input_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
class SelectPunishPerson extends StatefulWidget {
  @override
  _SelectPunishPersonState createState() => _SelectPunishPersonState();
}

class _SelectPunishPersonState extends State<SelectPunishPerson> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  final TextEditingController deductPoints=
  TextEditingController(); //分值
  SelectItems selectTeam = Get.arguments;
  List taskTypeList = ['警告','留用察看', '清除出场'];
  var selectTaskType = '请选择';

  List personList = [];
  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
    this.getPersons();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  /*
  * 获取工人
  * */
  getPersons() async {
    var result = await getWorkers({
      'workerTeamId': selectTeam.value,
    });
    if (result != null && result['results'].length > 0) {
      List list = [];
      result['results'].forEach((element) {
        list.add({'name':element['name'],'selected':false,'punishType':'','point':'','id':element['employeeId'],'punishTypeNum':''});
      });
      this.setState(() {
        personList = list;
      });
    }
    LogUtil.d(result);
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('处罚人员'),
      ),
      body:GestureDetector(
        onTap: (){
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          color: Styles.MainBackgroundColor,
          padding: EdgeInsets.fromLTRB(
              0, 0, 0, MediaQuery.of(context).padding.bottom + 70),
          child:SingleChildScrollView(
            child: Column(
              children: this.personList.map<Widget>((item){
                return Container(
                  color: Colors.white,
                  padding: EdgeInsets.fromLTRB(Styles.spacing16, Styles.spacing8, Styles.spacing16, Styles.spacing8),
                  child: Row(
                    children: [
                      Expanded(flex:1,child: GestureDetector(
                        behavior: HitTestBehavior.translucent,
                        onTap: (){
                          this.setState(() {
                            item['selected'] =
                            item['selected'] == null ? true : !item['selected'];
                            if(!item['selected']){
                              item['punishType'] = '';
                              item['punishTypeNum'] = '';
                              item['point'] = '';
                            }
                          });
                        },
                        child: Container(
                          alignment: Alignment.center,
                          height: Styles.MainButtonHeight,
                          margin: EdgeInsets.only(right: Styles.spacing12),
                          child: Row(
                            children: [
                              Container(
                                padding:
                                EdgeInsets.fromLTRB(0, 0, Styles.spacing8, 0),
                                child: Image.asset(
                                  (item['selected']!=null&&item['selected'])?"assets/images/group_selected.png":"assets/images/group_select.png",
                                  width: 20.w,
                                  height: 20.h,
                                  fit: BoxFit.contain,
                                ),
                              ),
                              Text(
                                item['name'],
                                style: TextStyle(
                                    color: Styles.DetailColor,
                                    fontSize: Styles.Middle16Font),
                              )
                            ],
                          ),
                        ),
                      )),
                      item['selected']?Expanded(flex:3,child: Row(
                        children: [
                          Expanded(flex:2,child: GestureDetector(
                            behavior: HitTestBehavior.translucent,
                            onTap: () {
                              Pickers.showSinglePicker(context,
                                  data: this.taskTypeList,
                                  selectData: item['punishType'],
                                  onConfirm: (p, position) {
                                    this.setState(() {
                                      item['punishType'] = p;
                                      if(p=='警告'){
                                        item['punishTypeNum'] = 1;
                                      }else if(p=='留用察看'){
                                        item['punishTypeNum'] = 3;
                                      }else{
                                        item['punishTypeNum'] = 4;
                                      }

                                    });
                                  }, onChanged: (value, _) {});
                            },
                            child: Container(
                              color: Styles.GrayLight,
                              margin: EdgeInsets.fromLTRB(0, 0, Styles.spacing4, 0),
                              padding: EdgeInsets.fromLTRB(
                                  Styles.spacing16,
                                  Styles.spacing8,
                                  Styles.spacing16,
                                  Styles.spacing8),
                              height: Styles.MainButtonHeight,
                              alignment: Alignment.center,
                              child: RichText(
                                  text: TextSpan(children: [
                                    TextSpan(
                                      text: item['punishType']==''?'请选择':item['punishType'],
                                      style: TextStyle(
                                          fontSize: Styles.NormalFont,
                                          color: Styles.DetailColor),
                                    ),
                                    TextSpan(
                                      text: ' ',
                                      style: TextStyle(
                                          fontSize: Styles.NormalFont,
                                          color: Styles.DetailColor),
                                    ),
                                    WidgetSpan(
                                      alignment: PlaceholderAlignment.middle,
                                      child: Image.asset(
                                        'assets/images/pull_down.png',
                                        width: 10.w,
                                        height: 10.h,
                                      ),
                                    )
                                  ])),
                            ),
                          ),),
                          Expanded(flex:2,child: Container(
                            height: Styles.MainButtonHeight,
                            padding: EdgeInsets.fromLTRB(
                                Styles.spacing8, 0, Styles.spacing8, 0),
                            child: CustomInput(
                              backColor: Styles.GrayLight,
                              keyboardType: TextInputType.number,
                              textFont: Styles.NormalFont,
                              inputformat: [LengthLimitingTextInputFormatter(3),WhitelistingTextInputFormatter(RegExp("[0-9]"))],
                              placeHold: '输入分值',
                              clearAll: false,
                              require: false,
                              textChange: (value){
                                item['point'] = value;
                              },
                            ),
                          ))
                        ],
                      )):Container()
                    ],
                  ),
                );
              }).toList(),
            ),
          ) ,
        ),
      ) ,
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: Container(
          padding: EdgeInsets.fromLTRB(
              Styles.spacing8, Styles.spacing8, Styles.spacing8, 10),
          child: Container(
            decoration: new BoxDecoration(
              //背景
              color: Styles.BlueColor,
              //设置四周圆角 角度
              borderRadius: BorderRadius.all(Radius.circular(12.0)),
            ),
            child: FlatButton(
              disabledColor: Styles.SelectColor,
              onPressed: () {
                List list = [];
                for(var i = 0;i<this.personList.length;i++){
                  if(this.personList[i]['selected']){
                    if(this.personList[i]['punishType']==''){
                      Fluttertoast.showToast(msg: '请选择${this.personList[i]['name']}的惩罚类型');
                      return;
                    }else if(this.personList[i]['point']==''){
                      Fluttertoast.showToast(msg: '请输入${this.personList[i]['name']}的扣除分值');
                      return;
                    }else{
                      list.add(this.personList[i]);
                    }
                  }
                }
                if(list.length==0){
                  Fluttertoast.showToast(msg: '请选择人员');
                }else{
                  Get.back(result: list);
                }
              },
              child: Container(
                height: Styles.MainButtonHeight,
                alignment: Alignment.center,
                child: Text(
                  '确定',
                  style: TextStyle(
                      fontSize: Styles.MainButtonTextFont, color: Colors.white),
                ),
              ),
            ),
          ),
        )
    );
  }
}
